package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Permission;
import cn.qihang.ssm.pojo.Role;
import cn.qihang.ssm.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/3 16:50
 * @Desc:
 */
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 查询角色
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(@RequestParam(name="page",defaultValue = "1")Integer page,
                                @RequestParam(name="size",defaultValue = "5")Integer size){

        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList = this.roleService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(roleList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("role-list");
        return modelAndView;

    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("save")
    public String save(Role role){
        this.roleService.save(role);
        return "redirect:findAll.do";
    }


    /**
     * 角色详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam("id") Integer id) {
        Role role = this.roleService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("role-show");
        return modelAndView;
    }


    /**
     * 添加资源权限前-查询哪些资源权限可以添加
     * @param id
     * @return
     */
    @RequestMapping("findRoleByIdAndAllPermission")
    public ModelAndView findRoleByIdAndAllPermission(@RequestParam("id") Integer id){
        //查询当使用角色是谁
        Role role = this.roleService.findById(id);

        //查询哪些资源权限可以添加
        List<Permission> permissionList =  this.roleService.findOtherPermission(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.addObject("permissionList",permissionList);
        modelAndView.setViewName("role-permission-add");
        return modelAndView;
    }


    /**
     * 为角色添加资源权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @RequestMapping("addPermissionToRole")
    public String addPermissionToRole(@RequestParam("roleId") Integer roleId, @RequestParam("ids") Integer[] permissionIds){
        this.roleService.addPermissionToRole(roleId,permissionIds);
        return "redirect:findAll.do";
    }


    /**
     * 删除角色
     * @return
     */
    @RequestMapping("deleteRole")
    public String deleteRole(@RequestParam("id") Integer roleId){
        this.roleService.deleteRoleById(roleId);
        return "redirect:findAll.do";
    }
}
